import java.util.Scanner;

/**
 * Created by IntelliJ
 * Author:熊磊
 */
public class HashTableDeomo {
    public static void main(String[] args) {
        int id=0;
        HashTable hashTable = new HashTable(8);
        String key="";
        Scanner scanner = new Scanner(System.in);
        boolean loop=true;
        while (loop){
            System.out.println("add: 添加雇员" );
            System.out.println("list 显示雇员");
            System.out.println("exit 退出");
            key=scanner.next();
            switch (key){
                case "add":
                    System.out.println("输入用户名字");
                    key=scanner.next();
                    hashTable.add(new Emp(id++,key));
                    break;
                case "list":
                    hashTable.list();
                    break;
                case "exit":
                    loop=false;
                    break;
                case "1":
                    hashTable.empLinkedListArray[0].list(0);
                    break;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }
    }
}
class HashTable{
    public EmpLinkedList[] empLinkedListArray;
    private int size;

    public HashTable(int size) {
        this.size=size;
        empLinkedListArray=new EmpLinkedList[size];
        //别忘了初始化链表
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i] =new EmpLinkedList();
        }
    }

    public void add(Emp emp){
        int hash = hash(emp.id);
        empLinkedListArray[hash].add(emp);
    }
    public void list(){
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i].list(i+1);
        }
    }

    public int hash(int id){
        //1111 1111 =16-1
        return id&(size-1);
    }
}
class Emp{
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }
}
class EmpLinkedList{
    private Emp head;

    //添加方法
    public void add(Emp emp){
        if (head==null){
            head=emp;
        }else{
            Emp temp = head;
            while (temp.next!=null){
                temp=temp.next;
            }
            temp.next=emp;
        }
    }

    public void list(int no){
        if (head==null){
            System.out.println("第"+no+"当前链表为空");
            return;
        }
        Emp temp=head;
        System.out.print("第"+no+"条链表信息为");
        while (temp!=null){
            System.out.printf("=> id=%d name=%s\t",temp.id,temp.name);
            temp=temp.next;
        }
        System.out.println();
    }
}